package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.ExcelUtils;
import com.feeyin.radar.commons.FileUtils;
import com.feeyin.radar.commons.Log;
import com.feeyin.radar.commons.NumberUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.CustomerUpdateHistoryDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.LinkmanUpdateHistoryDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.form.UploadFileForm;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.service.OpenCustomerService;
import com.feeyin.radar.sys.dao.CountryDAO;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Country;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.ManageSet;
import com.feeyin.radar.sys.model.ManageSetObject;
import com.feeyin.radar.sys.model.RecordRange;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.web.multipart.MultipartFile;

public class OpenCustomerServiceImpl
  implements OpenCustomerService
{
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;
  private CountryDAO countryDAO;
  private CustomerDAO customerDAO;
  private CustomerUpdateHistoryDAO customerUpdateHistoryDAO;
  private ManageSetDAO manageSetDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private CustomerShareDAO customerShareDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private SaleRecordDAO saleRecordDAO;
  private LinkmanDAO linkmanDAO;
  private LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO;
  private Map recordRanges = new HashMap();

  public DataDictionaryDAO getDataDictionaryDAO() {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public CountryDAO getCountryDAO() {
    return this.countryDAO;
  }

  public void setCountryDAO(CountryDAO countryDAO) {
    this.countryDAO = countryDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public CustomerUpdateHistoryDAO getCustomerUpdateHistoryDAO() {
    return this.customerUpdateHistoryDAO;
  }

  public void setCustomerUpdateHistoryDAO(CustomerUpdateHistoryDAO customerUpdateHistoryDAO)
  {
    this.customerUpdateHistoryDAO = customerUpdateHistoryDAO;
  }

  public ManageSetDAO getManageSetDAO() {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public LinkmanUpdateHistoryDAO getLinkmanUpdateHistoryDAO() {
    return this.linkmanUpdateHistoryDAO;
  }

  public void setLinkmanUpdateHistoryDAO(LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO)
  {
    this.linkmanUpdateHistoryDAO = linkmanUpdateHistoryDAO;
  }

  public Map getRecordRanges() {
    return this.recordRanges;
  }

  public void setRecordRanges(Map recordRanges) {
    this.recordRanges = recordRanges;
  }

  public List getPageOpenCustomers(String customerName, Integer currentUserId, Page page, QueryCustomer queryCustomer)
  {
    int isUseCommon = 0;
    isUseCommon = this.manageSetDAO.isUseCommons();
    if (isUseCommon == 0) return null;

    List openCustomers = new ArrayList();
    Customer customer = new Customer();
    customer.setCustomerCategory("0");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }

    if (isUseCommon == 2) {
      if (currentUserId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(currentUserId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (!StringUtils.isEmpty(deptIds)) {
        customer.setDeptIds2(deptIds);
      }
    }
    openCustomers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if ((openCustomers == null) || (openCustomers.size() <= 0)) return null;
    for (int i = 0; i < openCustomers.size(); i++) {
      Customer cCustomer = new Customer();
      cCustomer = (Customer)openCustomers.get(i);

      List linkmans = new ArrayList();
      linkmans = this.linkmanDAO.getLinkmansByCustomerId(cCustomer.getId());
      if ((linkmans == null) || (linkmans.size() <= 0))
        cCustomer.setLinkmanNum(Integer.valueOf(0));
      else {
        cCustomer.setLinkmanNum(Integer.valueOf(linkmans.size()));
      }

      cCustomer.setLinkNum(this.saleRecordDAO.getSaleRecordCountByCustomerId(cCustomer.getId()));

      cCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(cCustomer.getId()));
    }
    return openCustomers;
  }

  public Integer getPageOpenCustomersCount(String customerName, Integer currentUserId, QueryCustomer queryCustomer)
  {
    int isUseCommon = 0;
    isUseCommon = this.manageSetDAO.isUseCommons();
    if (isUseCommon == 0) return null;

    Integer counts = Integer.valueOf(0);
    Customer customer = new Customer();
    customer.setCustomerCategory("0");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName2())) {
        customer.setCustomerName(queryCustomer.getCustomerName2());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }

    if (isUseCommon == 2) {
      if (currentUserId == null) return null;
      Users user = new Users();
      user = this.userDAO.getUser(currentUserId);
      if (user == null) return null;
      String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
      if (!StringUtils.isEmpty(deptIds)) {
        customer.setDeptIds2(deptIds);
      }
    }
    counts = this.customerDAO.getPageCustomersCountByDynamic(customer);
    return counts;
  }

  public boolean protectCustomer(Integer currentUserId, String[] ids) {
    if (currentUserId == null) return false;
    if ((ids == null) || (ids.length <= 0)) return false;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("1");
    customer.setSalePersonId(currentUserId);
    customer.setUpdateCustomerDate(new Date());
    customer.setUpdateSaleRecordDate(new Date());
    customer.setUpdateSaleStageDate(new Date());
    customer.setSaleStageId(Integer.valueOf(1));
    customer.setCustomerSourceId(Integer.valueOf(0));
    customer.setDeptId(user.getDeptId());

    for (int i = 0; i < ids.length; i++) {
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      this.customerDAO.updateCustomer(customer);

      Customer customer2 = new Customer();
      customer2 = this.customerDAO.getCustomerById(Integer.valueOf(Integer.parseInt(ids[i])));
      if (customer2 != null) {
        this.customerShareDAO.insertCustomerShare(customer2.getId(), customer2.getSalePersonId(), "1");
      }
    }
    return true;
  }

  public List batchImportCustomer(Integer currentUserId, UploadFileForm uploadFileForm, String realPath, MultipartFile multipartFile, int sign)
  {
    boolean result1 = false;
    boolean result2 = false;

    String fileName = multipartFile.getOriginalFilename();
    String basePath = realPath + "upload" + System.getProperty("file.separator") + "temp";
    String storedPath = basePath + System.getProperty("file.separator") + fileName;
    result1 = FileUtils.saveFile(storedPath, multipartFile);

    List logs = new ArrayList();
    if (result1) {
      if (sign == 2)
        logs = readExcelContentByMarkettingCustomer(storedPath, currentUserId);
      else {
        logs = readExcelContent(storedPath, currentUserId, sign);
      }
    }

    return logs;
  }

  private List readExcelContent(String excelPath, Integer currentUserId, int sign)
  {
    POIFSFileSystem fileSystem = null;
    HSSFWorkbook workbook = null;
    HSSFSheet sheet = null;
    HSSFRow row = null;
    FileInputStream fileInputStream = null;

    List logs = new ArrayList();
    this.recordRanges = getCustomerDictionaryRanges();
    this.recordRanges = this.linkmanDAO.getLinkmanDictionaryRanges();
    try
    {
      fileInputStream = new FileInputStream(new File(excelPath));
      fileSystem = new POIFSFileSystem(fileInputStream);
      workbook = new HSSFWorkbook(fileSystem);
      sheet = workbook.getSheetAt(0);
      int rowNum = sheet.getLastRowNum();
      row = sheet.getRow(0);
      int colNum = row.getPhysicalNumberOfCells();
      for (int i = 1; i <= rowNum; i++) {
        row = sheet.getRow(i);
        int result = 0;
        Customer customer = new Customer();
        customer.setCustomerCode(DateUtils.getDateByFormat("yyyyMMddHHmmss") + i);
        String[] colContents = new String[80];
        Log log = new Log();
        colContents[0] = ExcelUtils.getStringCellValue(row.getCell((short)0)).trim();
        if (!StringUtils.isEmpty(colContents[0]))
          customer.setCustomerName(colContents[0]);
        else {
          result = 5;
        }
        colContents[1] = ExcelUtils.getStringCellValue(row.getCell((short)1)).trim();
        if (!StringUtils.isEmpty(colContents[1])) {
          customer.setAddress(colContents[1]);
        }
        colContents[2] = ExcelUtils.getStringCellValue(row.getCell((short)2)).trim();
        if (!StringUtils.isEmpty(colContents[2]))
          customer.setPhone(colContents[2]);
        else {
          result = 5;
        }
        colContents[3] = ExcelUtils.getStringCellValue(row.getCell((short)3)).trim();
        if (!StringUtils.isEmpty(colContents[3]))
          customer.setFax(colContents[3]);
        try
        {
          colContents[4] = ExcelUtils.getDateCellValue(row.getCell((short)4)).trim();

          if (!StringUtils.isEmpty(colContents[4]))
            customer.setPredictContract(DateUtils.stringDateConvertDate(colContents[4], "yyyy-MM-dd"));
        }
        catch (Exception ex) {
          result = 4;
        }
        colContents[5] = ExcelUtils.getStringCellValue(row.getCell((short)5)).trim();
        if (!StringUtils.isEmpty(colContents[5])) {
          customer.setPredictCost(NumberUtils.getBigDecimalByLong(colContents[5]));
        }

        colContents[6] = ExcelUtils.getStringCellValue(row.getCell((short)6)).trim();
        if (!StringUtils.isEmpty(colContents[6])) {
          customer.setHistoryContractCost(NumberUtils.getBigDecimalByLong(colContents[6]));
        }

        colContents[7] = ExcelUtils.getStringCellValue(row.getCell((short)7)).trim();
        if (!StringUtils.isEmpty(colContents[7])) {
          customer.setRequireDesc(colContents[7]);
        }
        colContents[8] = ExcelUtils.getStringCellValue(row.getCell((short)8)).trim();
        if (!StringUtils.isEmpty(colContents[8])) {
          customer.setIndustryId(getCustomerRecordRangeId("110", colContents[8]));
        }
        colContents[9] = ExcelUtils.getStringCellValue(row.getCell((short)9)).trim();
        if (!StringUtils.isEmpty(colContents[9])) {
          customer.setSubIndustryId(getCustomerRecordRangeId("127", colContents[9]));
        }
        colContents[10] = ExcelUtils.getStringCellValue(row.getCell((short)10)).trim();
        if (!StringUtils.isEmpty(colContents[10])) {
          customer.setAreaId(getCustomerRecordRangeId("111", colContents[10]));
        }
        colContents[11] = ExcelUtils.getStringCellValue(row.getCell((short)11)).trim();
        if (!StringUtils.isEmpty(colContents[11])) {
          customer.setOutputValueId(getCustomerRecordRangeId("112", colContents[11]));
        }
        colContents[12] = ExcelUtils.getStringCellValue(row.getCell((short)12)).trim();
        if (!StringUtils.isEmpty(colContents[12])) {
          customer.setCompanyTypeId(getCustomerRecordRangeId("113", colContents[12]));
        }
        colContents[13] = ExcelUtils.getStringCellValue(row.getCell((short)13)).trim();
        if (!StringUtils.isEmpty(colContents[13])) {
          customer.setHttp(colContents[13]);
        }
        colContents[14] = ExcelUtils.getStringCellValue(row.getCell((short)14)).trim();
        if (!StringUtils.isEmpty(colContents[14])) {
          customer.setProfitsId(getCustomerRecordRangeId("115", colContents[14]));
        }
        colContents[15] = ExcelUtils.getStringCellValue(row.getCell((short)15)).trim();
        if (!StringUtils.isEmpty(colContents[15])) {
          customer.setEmployeeNumId(getCustomerRecordRangeId("116", colContents[15]));
        }
        colContents[16] = ExcelUtils.getStringCellValue(row.getCell((short)16)).trim();
        if (!StringUtils.isEmpty(colContents[16])) {
          customer.setCustomerSimple(colContents[16]);
        }
        colContents[17] = ExcelUtils.getStringCellValue(row.getCell((short)17)).trim();
        if (!StringUtils.isEmpty(colContents[17])) {
          customer.setTaxCode(colContents[17]);
        }
        colContents[18] = ExcelUtils.getStringCellValue(row.getCell((short)18)).trim();
        if (!StringUtils.isEmpty(colContents[18])) {
          customer.setBankDepositId(getCustomerRecordRangeId("119", colContents[18]));
        }
        colContents[19] = ExcelUtils.getStringCellValue(row.getCell((short)19)).trim();
        if (!StringUtils.isEmpty(colContents[19])) {
          customer.setZip(colContents[19]);
        }
        colContents[20] = ExcelUtils.getStringCellValue(row.getCell((short)20)).trim();
        if (!StringUtils.isEmpty(colContents[20])) {
          customer.setIsMarketId(getCustomerRecordRangeId("121", colContents[20]));
        }
        colContents[21] = ExcelUtils.getStringCellValue(row.getCell((short)21)).trim();
        if (!StringUtils.isEmpty(colContents[21])) {
          customer.setIsPurchaseId(getCustomerRecordRangeId("122", colContents[21]));
        }
        colContents[22] = ExcelUtils.getStringCellValue(row.getCell((short)22)).trim();
        if (!StringUtils.isEmpty(colContents[22])) {
          customer.setCustomerTypeId(getCustomerRecordRangeId("123", colContents[22]));
        }
        colContents[23] = ExcelUtils.getStringCellValue(row.getCell((short)23)).trim();
        if (!StringUtils.isEmpty(colContents[23])) {
          customer.setCountryId(getCustomerRecordRangeId("124", colContents[23]));
        }
        colContents[24] = ExcelUtils.getStringCellValue(row.getCell((short)24)).trim();
        if (!StringUtils.isEmpty(colContents[24])) {
          customer.setProvinceId(getCustomerRecordRangeId("125", colContents[24]));
        }
        colContents[25] = ExcelUtils.getStringCellValue(row.getCell((short)25)).trim();
        if (!StringUtils.isEmpty(colContents[25])) {
          customer.setCityId(getCustomerRecordRangeId("126", colContents[25]));
        }
        colContents[26] = ExcelUtils.getStringCellValue(row.getCell((short)26)).trim();
        if (!StringUtils.isEmpty(colContents[26])) {
          customer.setClusterId(getCustomerRecordRangeId("128", colContents[26]));
        }
        colContents[27] = ExcelUtils.getStringCellValue(row.getCell((short)27)).trim();
        if (!StringUtils.isEmpty(colContents[27])) {
          customer.setScaleId(getCustomerRecordRangeId("129", colContents[27]));
        }
        colContents[28] = ExcelUtils.getStringCellValue(row.getCell((short)28)).trim();
        if (!StringUtils.isEmpty(colContents[28])) {
          customer.setParentCompany(colContents[28]);
        }

        Linkman[] linkmans = new Linkman[4];
        colContents[29] = ExcelUtils.getStringCellValue(row.getCell((short)29)).trim();
        if (!StringUtils.isEmpty(colContents[29])) {
          linkmans[0] = new Linkman();
          linkmans[0].setLinkmanName(colContents[29]);

          colContents[30] = ExcelUtils.getStringCellValue(row.getCell((short)30)).trim();
          if (!StringUtils.isEmpty(colContents[30]))
            linkmans[0].setLinkmanDept(colContents[30]);
          else {
            result = 12;
          }
          colContents[31] = ExcelUtils.getStringCellValue(row.getCell((short)31)).trim();
          if (!StringUtils.isEmpty(colContents[31])) {
            linkmans[0].setLinkmanDuty(colContents[31]);
          }
          colContents[32] = ExcelUtils.getStringCellValue(row.getCell((short)32)).trim();
          if (!StringUtils.isEmpty(colContents[32])) {
            linkmans[0].setItemRoleId(getLinkmanRecordRangeId("24", colContents[32]));
          }
          colContents[33] = ExcelUtils.getStringCellValue(row.getCell((short)33)).trim();
          if (!StringUtils.isEmpty(colContents[33])) {
            linkmans[0].setMobile(colContents[33]);
          }
          colContents[34] = ExcelUtils.getStringCellValue(row.getCell((short)34)).trim();
          if (!StringUtils.isEmpty(colContents[34])) {
            linkmans[0].setOfficePhone(colContents[34]);
          }
          colContents[35] = ExcelUtils.getStringCellValue(row.getCell((short)35)).trim();
          if (!StringUtils.isEmpty(colContents[35])) {
            linkmans[0].setEmail(colContents[35]);
          }
          colContents[36] = ExcelUtils.getStringCellValue(row.getCell((short)36)).trim();
          if (!StringUtils.isEmpty(colContents[36])) {
            linkmans[0].setSexId(getLinkmanRecordRangeId("29", colContents[36]));
          }
          colContents[37] = ExcelUtils.getStringCellValue(row.getCell((short)37)).trim();
          if (!StringUtils.isEmpty(colContents[37])) {
            linkmans[0].setHomePhone(colContents[37]);
          }
          colContents[38] = ExcelUtils.getStringCellValue(row.getCell((short)38)).trim();
          if (!StringUtils.isEmpty(colContents[38])) {
            linkmans[0].setMSN(colContents[38]);
          }
          colContents[39] = ExcelUtils.getStringCellValue(row.getCell((short)39)).trim();
          if (!StringUtils.isEmpty(colContents[39]))
            linkmans[0].setQQ(colContents[39]);
          try
          {
            colContents[40] = ExcelUtils.getDateCellValue(row.getCell((short)40)).trim();
            if (!StringUtils.isEmpty(colContents[40]))
              linkmans[0].setBirthday(DateUtils.stringDateConvertDate(colContents[40], "yyyy-MM-dd"));
          }
          catch (Exception ex) {
            result = 13;
          }
          colContents[41] = ExcelUtils.getStringCellValue(row.getCell((short)41)).trim();
          if (!StringUtils.isEmpty(colContents[41])) {
            linkmans[0].setInterestId(getLinkmanRecordRangeId("221", colContents[41]));
          }
          colContents[42] = ExcelUtils.getStringCellValue(row.getCell((short)42)).trim();
          if (!StringUtils.isEmpty(colContents[42])) {
            linkmans[0].setLinkDesc(colContents[42]);
          }

        }

        colContents[43] = ExcelUtils.getStringCellValue(row.getCell((short)43)).trim();
        if (!StringUtils.isEmpty(colContents[43])) {
          linkmans[1] = new Linkman();
          linkmans[1].setLinkmanName(colContents[43]);

          colContents[44] = ExcelUtils.getStringCellValue(row.getCell((short)44)).trim();
          if (!StringUtils.isEmpty(colContents[44]))
            linkmans[1].setLinkmanDept(colContents[44]);
          else {
            result = 22;
          }
          colContents[45] = ExcelUtils.getStringCellValue(row.getCell((short)45)).trim();
          if (!StringUtils.isEmpty(colContents[45])) {
            linkmans[1].setLinkmanDuty(colContents[45]);
          }
          colContents[46] = ExcelUtils.getStringCellValue(row.getCell((short)46)).trim();
          if (!StringUtils.isEmpty(colContents[46])) {
            linkmans[1].setItemRoleId(getLinkmanRecordRangeId("24", colContents[46]));
          }
          colContents[47] = ExcelUtils.getStringCellValue(row.getCell((short)47)).trim();
          if (!StringUtils.isEmpty(colContents[47])) {
            linkmans[1].setMobile(colContents[47]);
          }
          colContents[48] = ExcelUtils.getStringCellValue(row.getCell((short)48)).trim();
          if (!StringUtils.isEmpty(colContents[48])) {
            linkmans[1].setOfficePhone(colContents[48]);
          }
          colContents[49] = ExcelUtils.getStringCellValue(row.getCell((short)49)).trim();
          if (!StringUtils.isEmpty(colContents[49])) {
            linkmans[1].setEmail(colContents[49]);
          }
          colContents[50] = ExcelUtils.getStringCellValue(row.getCell((short)50)).trim();
          if (!StringUtils.isEmpty(colContents[50])) {
            linkmans[1].setSexId(getLinkmanRecordRangeId("29", colContents[50]));
          }
          colContents[51] = ExcelUtils.getStringCellValue(row.getCell((short)51)).trim();
          if (!StringUtils.isEmpty(colContents[51])) {
            linkmans[1].setHomePhone(colContents[51]);
          }
          colContents[52] = ExcelUtils.getStringCellValue(row.getCell((short)52)).trim();
          if (!StringUtils.isEmpty(colContents[52])) {
            linkmans[1].setMSN(colContents[52]);
          }
          colContents[53] = ExcelUtils.getStringCellValue(row.getCell((short)53)).trim();
          if (!StringUtils.isEmpty(colContents[53]))
            linkmans[1].setQQ(colContents[53]);
          try
          {
            colContents[54] = ExcelUtils.getDateCellValue(row.getCell((short)54)).trim();
            if (!StringUtils.isEmpty(colContents[54]))
              linkmans[1].setBirthday(DateUtils.stringDateConvertDate(colContents[54], "yyyy-MM-dd"));
          }
          catch (Exception ex) {
            result = 23;
          }
          colContents[55] = ExcelUtils.getStringCellValue(row.getCell((short)55)).trim();
          if (!StringUtils.isEmpty(colContents[55])) {
            linkmans[1].setInterestId(getLinkmanRecordRangeId("221", colContents[55]));
          }
          colContents[56] = ExcelUtils.getStringCellValue(row.getCell((short)56)).trim();
          if (!StringUtils.isEmpty(colContents[56])) {
            linkmans[1].setLinkDesc(colContents[56]);
          }

        }

        colContents[57] = ExcelUtils.getStringCellValue(row.getCell((short)57)).trim();
        if (!StringUtils.isEmpty(colContents[57])) {
          linkmans[2] = new Linkman();
          linkmans[2].setLinkmanName(colContents[57]);

          colContents[58] = ExcelUtils.getStringCellValue(row.getCell((short)58)).trim();
          if (!StringUtils.isEmpty(colContents[58]))
            linkmans[2].setLinkmanDept(colContents[58]);
          else {
            result = 32;
          }
          colContents[59] = ExcelUtils.getStringCellValue(row.getCell((short)59)).trim();
          if (!StringUtils.isEmpty(colContents[59])) {
            linkmans[2].setLinkmanDuty(colContents[59]);
          }
          colContents[60] = ExcelUtils.getStringCellValue(row.getCell((short)60)).trim();
          if (!StringUtils.isEmpty(colContents[60])) {
            linkmans[2].setItemRoleId(getLinkmanRecordRangeId("24", colContents[60]));
          }
          colContents[61] = ExcelUtils.getStringCellValue(row.getCell((short)61)).trim();
          if (!StringUtils.isEmpty(colContents[61])) {
            linkmans[2].setMobile(colContents[61]);
          }
          colContents[62] = ExcelUtils.getStringCellValue(row.getCell((short)62)).trim();
          if (!StringUtils.isEmpty(colContents[62])) {
            linkmans[2].setOfficePhone(colContents[62]);
          }
          colContents[63] = ExcelUtils.getStringCellValue(row.getCell((short)63)).trim();
          if (!StringUtils.isEmpty(colContents[63])) {
            linkmans[2].setEmail(colContents[63]);
          }
          colContents[64] = ExcelUtils.getStringCellValue(row.getCell((short)64)).trim();
          if (!StringUtils.isEmpty(colContents[64])) {
            linkmans[2].setSexId(getLinkmanRecordRangeId("29", colContents[64]));
          }
          colContents[65] = ExcelUtils.getStringCellValue(row.getCell((short)65)).trim();
          if (!StringUtils.isEmpty(colContents[65])) {
            linkmans[2].setHomePhone(colContents[65]);
          }
          colContents[66] = ExcelUtils.getStringCellValue(row.getCell((short)66)).trim();
          if (!StringUtils.isEmpty(colContents[66])) {
            linkmans[2].setMSN(colContents[66]);
          }
          colContents[67] = ExcelUtils.getStringCellValue(row.getCell((short)67)).trim();
          if (!StringUtils.isEmpty(colContents[67]))
            linkmans[2].setQQ(colContents[67]);
          try
          {
            colContents[68] = ExcelUtils.getDateCellValue(row.getCell((short)68)).trim();
            if (!StringUtils.isEmpty(colContents[68]))
              linkmans[2].setBirthday(DateUtils.stringDateConvertDate(colContents[68], "yyyy-MM-dd"));
          }
          catch (Exception ex) {
            result = 33;
          }
          colContents[69] = ExcelUtils.getStringCellValue(row.getCell((short)69)).trim();
          if (!StringUtils.isEmpty(colContents[69])) {
            linkmans[2].setInterestId(getLinkmanRecordRangeId("221", colContents[69]));
          }
          colContents[70] = ExcelUtils.getStringCellValue(row.getCell((short)70)).trim();
          if (!StringUtils.isEmpty(colContents[70])) {
            linkmans[2].setLinkDesc(colContents[70]);
          }

        }

        log.setId(Integer.valueOf(i + 1));
        if (result == 5) {
          log.setReason("5");
        } else if (result == 4) {
          log.setReason("4");
        } else if (result == 12) {
          log.setReason("12");
        } else if (result == 13) {
          log.setReason("13");
        } else if (result == 22) {
          log.setReason("22");
        } else if (result == 23) {
          log.setReason("23");
        } else if (result == 32) {
          log.setReason("32");
        } else if (result == 33) {
          log.setReason("33");
        } else {
          result = insertCustomer(customer, currentUserId, sign, linkmans);
          if (result == 2)
            log.setReason("2");
          else if (result == 1)
            log.setReason("1");
          else if (result == 0) {
            log.setReason("0");
          }
        }
        logs.add(log);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    return logs;
  }

  private List readExcelContentByMarkettingCustomer(String excelPath, Integer currentUserId)
  {
    POIFSFileSystem fileSystem = null;
    HSSFWorkbook workbook = null;
    HSSFSheet sheet = null;
    HSSFRow row = null;
    FileInputStream fileInputStream = null;

    List logs = new ArrayList();
    this.recordRanges = getCustomerDictionaryRanges();
    try
    {
      fileInputStream = new FileInputStream(new File(excelPath));
      fileSystem = new POIFSFileSystem(fileInputStream);
      workbook = new HSSFWorkbook(fileSystem);
      sheet = workbook.getSheetAt(0);
      int rowNum = sheet.getLastRowNum();
      row = sheet.getRow(0);
      int colNum = row.getPhysicalNumberOfCells();
      for (int i = 1; i <= rowNum; i++) {
        row = sheet.getRow(i);
        int result = 0;
        Customer customer = new Customer();
        customer.setCustomerCode(DateUtils.getDateByFormat("yyyyMMddHHmmss") + i);
        String[] colContents = new String[35];
        Log log = new Log();
        colContents[0] = ExcelUtils.getStringCellValue(row.getCell((short)0)).trim();
        if (!StringUtils.isEmpty(colContents[0]))
          customer.setCustomerName(colContents[0]);
        else {
          result = 5;
        }
        colContents[1] = ExcelUtils.getStringCellValue(row.getCell((short)1)).trim();
        if (!StringUtils.isEmpty(colContents[1])) {
          customer.setAddress(colContents[1]);
        }
        colContents[2] = ExcelUtils.getStringCellValue(row.getCell((short)2)).trim();
        if (!StringUtils.isEmpty(colContents[2]))
          customer.setPhone(colContents[2]);
        else {
          result = 5;
        }
        colContents[3] = ExcelUtils.getStringCellValue(row.getCell((short)3)).trim();
        if (!StringUtils.isEmpty(colContents[3])) {
          customer.setFax(colContents[3]);
        }
        colContents[4] = ExcelUtils.getStringCellValue(row.getCell((short)4)).trim();
        if (!StringUtils.isEmpty(colContents[4]))
          customer.setPresentRecord(colContents[4]);
        else {
          result = 5;
        }
        colContents[5] = ExcelUtils.getStringCellValue(row.getCell((short)5)).trim();
        if (!StringUtils.isEmpty(colContents[5])) {
          customer.setMarketPersonId(getSalePersonIdByName(colContents[5]));
        }
        colContents[6] = ExcelUtils.getStringCellValue(row.getCell((short)6)).trim();
        if (!StringUtils.isEmpty(colContents[6]))
        {
          customer.setSaleStageId(getSaleStageIdByName(colContents[6]));
        }
        try {
          colContents[7] = ExcelUtils.getDateCellValue(row.getCell((short)7)).trim();
          if (!StringUtils.isEmpty(colContents[7]))
            customer.setPredictContract(DateUtils.stringDateConvertDate(colContents[7], "yyyy-MM-dd"));
        }
        catch (Exception ex) {
          result = 4;
        }
        colContents[8] = ExcelUtils.getStringCellValue(row.getCell((short)8)).trim();
        if (!StringUtils.isEmpty(colContents[8])) {
          customer.setPredictCost(NumberUtils.getBigDecimalByLong(colContents[8]));
        }
        colContents[9] = ExcelUtils.getStringCellValue(row.getCell((short)9)).trim();
        if (!StringUtils.isEmpty(colContents[9])) {
          customer.setRequireDesc(colContents[9]);
        }
        colContents[10] = ExcelUtils.getStringCellValue(row.getCell((short)10)).trim();
        if (!StringUtils.isEmpty(colContents[10])) {
          customer.setIndustryId(getCustomerRecordRangeId("110", colContents[10]));
        }
        colContents[11] = ExcelUtils.getStringCellValue(row.getCell((short)11)).trim();
        if (!StringUtils.isEmpty(colContents[11])) {
          customer.setSubIndustryId(getCustomerRecordRangeId("127", colContents[11]));
        }
        colContents[12] = ExcelUtils.getStringCellValue(row.getCell((short)12)).trim();
        if (!StringUtils.isEmpty(colContents[12])) {
          customer.setAreaId(getCustomerRecordRangeId("111", colContents[12]));
        }
        colContents[13] = ExcelUtils.getStringCellValue(row.getCell((short)13)).trim();
        if (!StringUtils.isEmpty(colContents[13])) {
          customer.setOutputValueId(getCustomerRecordRangeId("112", colContents[13]));
        }
        colContents[14] = ExcelUtils.getStringCellValue(row.getCell((short)14)).trim();
        if (!StringUtils.isEmpty(colContents[14])) {
          customer.setCompanyTypeId(getCustomerRecordRangeId("113", colContents[14]));
        }
        colContents[15] = ExcelUtils.getStringCellValue(row.getCell((short)15)).trim();
        if (!StringUtils.isEmpty(colContents[15])) {
          customer.setHttp(colContents[15]);
        }
        colContents[16] = ExcelUtils.getStringCellValue(row.getCell((short)16)).trim();
        if (!StringUtils.isEmpty(colContents[16])) {
          customer.setProfitsId(getCustomerRecordRangeId("115", colContents[16]));
        }
        colContents[17] = ExcelUtils.getStringCellValue(row.getCell((short)17)).trim();
        if (!StringUtils.isEmpty(colContents[17])) {
          customer.setEmployeeNumId(getCustomerRecordRangeId("116", colContents[17]));
        }
        colContents[18] = ExcelUtils.getStringCellValue(row.getCell((short)18)).trim();
        if (!StringUtils.isEmpty(colContents[18])) {
          customer.setCustomerSimple(colContents[18]);
        }
        colContents[19] = ExcelUtils.getStringCellValue(row.getCell((short)19)).trim();
        if (!StringUtils.isEmpty(colContents[19])) {
          customer.setTaxCode(colContents[19]);
        }
        colContents[20] = ExcelUtils.getStringCellValue(row.getCell((short)20)).trim();
        if (!StringUtils.isEmpty(colContents[20])) {
          customer.setBankDepositId(getCustomerRecordRangeId("119", colContents[20]));
        }
        colContents[21] = ExcelUtils.getStringCellValue(row.getCell((short)21)).trim();
        if (!StringUtils.isEmpty(colContents[21])) {
          customer.setZip(colContents[21]);
        }
        colContents[22] = ExcelUtils.getStringCellValue(row.getCell((short)22)).trim();
        if (!StringUtils.isEmpty(colContents[22])) {
          customer.setIsMarketId(getCustomerRecordRangeId("121", colContents[22]));
        }
        colContents[23] = ExcelUtils.getStringCellValue(row.getCell((short)23)).trim();
        if (!StringUtils.isEmpty(colContents[23])) {
          customer.setIsPurchaseId(getCustomerRecordRangeId("122", colContents[23]));
        }
        colContents[24] = ExcelUtils.getStringCellValue(row.getCell((short)24)).trim();
        if (!StringUtils.isEmpty(colContents[24])) {
          customer.setCustomerTypeId(getCustomerRecordRangeId("123", colContents[24]));
        }
        colContents[25] = ExcelUtils.getStringCellValue(row.getCell((short)25)).trim();
        if (!StringUtils.isEmpty(colContents[25])) {
          customer.setCountryId(getCustomerRecordRangeId("124", colContents[25]));
        }
        colContents[26] = ExcelUtils.getStringCellValue(row.getCell((short)26)).trim();
        if (!StringUtils.isEmpty(colContents[26])) {
          customer.setProvinceId(getCustomerRecordRangeId("125", colContents[26]));
        }
        colContents[27] = ExcelUtils.getStringCellValue(row.getCell((short)27)).trim();
        if (!StringUtils.isEmpty(colContents[27])) {
          customer.setCityId(getCustomerRecordRangeId("126", colContents[27]));
        }
        colContents[28] = ExcelUtils.getStringCellValue(row.getCell((short)28)).trim();
        if (!StringUtils.isEmpty(colContents[28])) {
          customer.setClusterId(getCustomerRecordRangeId("128", colContents[28]));
        }
        colContents[29] = ExcelUtils.getStringCellValue(row.getCell((short)29)).trim();
        if (!StringUtils.isEmpty(colContents[29])) {
          customer.setScaleId(getCustomerRecordRangeId("129", colContents[29]));
        }
        colContents[30] = ExcelUtils.getStringCellValue(row.getCell((short)30)).trim();
        if (!StringUtils.isEmpty(colContents[30])) {
          customer.setParentCompany(colContents[30]);
        }

        log.setId(Integer.valueOf(i + 1));
        if (result == 5) {
          log.setReason("5");
        } else if (result == 4) {
          log.setReason("4");
        } else {
          result = insertCustomer(customer, currentUserId, 2, null);
          if (result == 2)
            log.setReason("2");
          else if (result == 1)
            log.setReason("1");
          else if (result == 0) {
            log.setReason("0");
          }
        }
        logs.add(log);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        if (fileInputStream != null)
          fileInputStream.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    return logs;
  }

  private Map getCustomerDictionaryRanges()
  {
    Map map = new HashMap();

    List customerDictionarys = new ArrayList();
    customerDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("1");
    if (customerDictionarys != null) {
      for (int i = 0; i < customerDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)customerDictionarys.get(i);
        List ranges = this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId());
        if ("124".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByParentId(Integer.valueOf(0));
        else if ("125".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(2));
        else if ("126".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(3));
        else if ("127".equals(dataDictionary.getRecordCode())) {
          ranges = this.recordRangeDAO.getActivityRecordRangesByLevel(Integer.valueOf(2));
        }
        map.put(dataDictionary.getRecordCode(), ranges);
      }
    }
    return map;
  }

  private Integer getCustomerRecordRangeId(String recordCode, String rangeValue)
  {
    Integer recordRangeId = new Integer(0);
    if ((StringUtils.isEmpty(recordCode)) || (StringUtils.isEmpty(rangeValue))) return recordRangeId;

    Map map = new HashMap();
    map = getRecordRanges();
    if ((map == null) || (map.get(recordCode) == null)) return recordRangeId;

    List ranges = new ArrayList();
    ranges = (List)map.get(recordCode);
    if (ranges == null) return recordRangeId;
    if (("124".equals(recordCode)) || ("125".equals(recordCode)) || ("126".equals(recordCode))) {
      for (int i = 0; i < ranges.size(); i++) {
        Country country = new Country();
        country = (Country)ranges.get(i);
        if ((country != null) && (rangeValue.equals(country.getCName())))
          recordRangeId = country.getId();
      }
    }
    else {
      for (int i = 0; i < ranges.size(); i++) {
        RecordRange recordRange = new RecordRange();
        recordRange = (RecordRange)ranges.get(i);
        if ((recordRange != null) && (rangeValue.equals(recordRange.getRangeValue()))) {
          recordRangeId = recordRange.getId();
        }
      }
    }

    return recordRangeId;
  }

  private Integer getLinkmanRecordRangeId(String recordCode, String rangeValue)
  {
    Integer recordRangeId = new Integer(0);
    if ((StringUtils.isEmpty(recordCode)) || (StringUtils.isEmpty(rangeValue))) return recordRangeId;

    Map map = new HashMap();
    map = getRecordRanges();
    if ((map == null) || (map.get(recordCode) == null)) return recordRangeId;

    List ranges = new ArrayList();
    ranges = (List)map.get(recordCode);
    if (ranges == null) return recordRangeId;
    if (("217".equals(recordCode)) || ("218".equals(recordCode)) || ("219".equals(recordCode))) {
      for (int i = 0; i < ranges.size(); i++) {
        Country country = new Country();
        country = (Country)ranges.get(i);
        if ((country != null) && (rangeValue.equals(country.getCName())))
          recordRangeId = country.getId();
      }
    }
    else {
      for (int i = 0; i < ranges.size(); i++) {
        RecordRange recordRange = new RecordRange();
        recordRange = (RecordRange)ranges.get(i);
        if ((recordRange != null) && (rangeValue.equals(recordRange.getRangeValue()))) {
          recordRangeId = recordRange.getId();
        }
      }
    }

    return recordRangeId;
  }

  private Integer getSalePersonIdByName(String name)
  {
    Integer salePersonId = Integer.valueOf(0);
    if (StringUtils.isEmpty(name)) return Integer.valueOf(0);

    List users = new ArrayList();
    Users user = new Users();
    user.setIsActivity("1");
    user.setUserName(name);
    users = this.userDAO.getUsersByDynamic(user);
    if ((users == null) || (users.size() <= 0)) return Integer.valueOf(0);
    salePersonId = ((Users)users.get(0)).getId();

    return salePersonId;
  }

  private Integer getSaleStageIdByName(String name)
  {
    Integer saleStageId = Integer.valueOf(0);
    if (StringUtils.isEmpty(name)) return Integer.valueOf(0);

    List list = new ArrayList();
    list = this.saleStageSetDAO.getSaleStages();
    if ((list == null) || (list.size() < 0)) return Integer.valueOf(0);

    for (int i = 0; i < list.size(); i++) {
      SaleStageSet saleStage = new SaleStageSet();
      saleStage = (SaleStageSet)list.get(i);
      if ((!name.equals(saleStage.getStageName())) || 
        (StringUtils.isEmpty(saleStage.getStageCode()))) continue;
      saleStageId = Integer.valueOf(Integer.parseInt(saleStage.getStageCode()));
      break;
    }

    return saleStageId;
  }

  private List isRepeatSet()
  {
    List manageSetObjects = new ArrayList();

    ManageSet mSet = new ManageSet();
    ManageSet manageSet = new ManageSet();
    manageSet.setMType("5");
    mSet = this.manageSetDAO.getManageSetByTypeStatus(manageSet);
    if ((mSet != null) && ("0".equals(mSet.getStatus()))) {
      manageSetObjects = this.manageSetDAO.getManageSetObjectByManageSetId(mSet.getId());
    }

    return manageSetObjects;
  }

  private boolean isRepeatAddCustomer(Customer customer)
  {
    if (customer == null) return false;

    List list = new ArrayList();
    list = isRepeatSet();
    if (list == null) return false;
    Customer rCustomer = new Customer();
    for (int i = 0; i < list.size(); i++) {
      ManageSetObject manageSetObject = new ManageSetObject();
      manageSetObject = (ManageSetObject)list.get(i);
      if (manageSetObject.getOtherId().intValue() == 2)
        rCustomer.setCustomerName(customer.getCustomerName());
      else if (manageSetObject.getOtherId().intValue() == 3)
        rCustomer.setAddress(customer.getAddress());
      else if (manageSetObject.getOtherId().intValue() == 14)
        rCustomer.setHttp(customer.getHttp());
      else if (manageSetObject.getOtherId().intValue() == 4)
        rCustomer.setPhone(customer.getPhone());
      else if (manageSetObject.getOtherId().intValue() == 17)
        rCustomer.setCustomerSimple(customer.getCustomerSimple());
      else if (manageSetObject.getOtherId().intValue() == 5) {
        rCustomer.setFax(customer.getFax());
      }
    }
    list = new ArrayList();
    rCustomer.setSignCategory(Integer.valueOf(9));

    list = this.customerDAO.getRepeatCustomersByDynamic(rCustomer);
    return (list != null) && (list.size() > 0);
  }

  private int insertCustomer(Customer customer, Integer currentUserId, int sign, Linkman[] linkmans)
  {
    boolean result = false;
    int success = 0;
    if (customer == null) return success;
    if ((StringUtils.isEmpty(customer.getCustomerName())) || 
      (StringUtils.isEmpty(customer.getPhone()))) return success;
    if (isRepeatAddCustomer(customer)) return 1;

    if (sign == 0) {
      customer.setCustomerCategory("0");
      customer.setSaleStageId(Integer.valueOf(0));
    } else if (sign == 1) {
      customer.setCustomerCategory("7");
      customer.setSaleStageId(Integer.valueOf(0));
    } else if (sign == 2) {
      customer.setCustomerCategory("4");
      customer.setCustomerSourceId(Integer.valueOf(3));
    }

    customer.setIsNewAdd(currentUserId.toString());

    customer.setIsFollow("0");
    customer.setIsShare("0");
    customer.setCreateDate(new Date());
    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user != null) {
      customer.setDeptId(user.getDeptId());
    }
    result = this.customerDAO.insertCustomer(customer);
    if (result) success = 2;

    Customer customer2 = new Customer();
    customer2 = this.customerDAO.getCustomerByCustomerCode(customer.getCustomerCode());
    if (customer2 != null) {
      customer2.setCustomerId(customer2.getId());
      customer2.setUpdatePersonId(currentUserId);
      customer2.setUpdateDate(new Date());
      customer2.setIsPrimitive("1");
      this.customerUpdateHistoryDAO.insertCustomerUpdateHistory(customer2);

      if ((linkmans != null) && (linkmans.length > 0)) {
        for (int i = 0; i < linkmans.length; i++) {
          if (linkmans[i] != null) {
            linkmans[i].setCustomerId(customer2.getId());
            linkmans[i].setLinkmanCode(customer2.getId() + DateUtils.getDateByFormat("yyyyMMddHHmmss") + i);
            insertLinkman(linkmans[i]);
          }
        }
      }
    }

    return success;
  }

  private int insertLinkman(Linkman linkman)
  {
    int success = 0;
    if (linkman == null) return success;
    if (linkman.getCustomerId() == null) return success;

    List linkmanDictionarys = new ArrayList();
    linkmanDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("2");
    if (linkmanDictionarys != null) {
      for (int i = 0; i < linkmanDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)linkmanDictionarys.get(i);
        if (dataDictionary.getRecordCode().equals("22")) {
          linkman.setDeptDictionaryId(dataDictionary.getId());
        }
        if (dataDictionary.getRecordCode().equals("23")) {
          linkman.setDutyDictionaryId(dataDictionary.getId());
        }
      }
    }

    linkman.setCreateDate(new Date());

    linkman.setLinkmanDeptId(this.linkmanDAO.linkmanDeptId(linkman.getDeptDictionaryId(), linkman.getLinkmanDept()));
    linkman.setLinkmanDutyId(this.linkmanDAO.linkmanDutyId(linkman.getDutyDictionaryId(), linkman.getLinkmanDuty()));
    linkman.setStatus("0");

    Linkman lk = new Linkman();
    lk.setCustomerId(linkman.getCustomerId());
    lk.setLinkmanName(linkman.getLinkmanName());
    lk.setLinkmanDeptId(linkman.getLinkmanDeptId());
    List linkmans = new ArrayList();
    linkmans = this.linkmanDAO.getLinkmansByDynamic(lk);
    if ((linkmans != null) && (linkmans.size() > 0)) return 1;

    boolean result = this.linkmanDAO.insertLinkman(linkman);
    if (result) success = 2;

    Customer customer = new Customer();
    linkmans = new ArrayList();
    linkmans = this.linkmanDAO.getLinkmansByCustomerId(linkman.getCustomerId());
    if (linkmans != null)
      customer.setLinkmanNum(Integer.valueOf(linkmans.size()));
    else {
      customer.setLinkmanNum(Integer.valueOf(0));
    }
    customer.setId(linkman.getCustomerId());
    this.customerDAO.updateCustomer(customer);

    Linkman linkman2 = new Linkman();
    linkman2 = this.linkmanDAO.getLinkmanByLinkmanCode(linkman.getLinkmanCode());
    if (linkman2 != null) {
      linkman2.setLinkmanId(linkman2.getId());
      linkman2.setUpdatePersonId(linkman.getCurrentUserId());
      linkman2.setUpdateDate(new Date());
      linkman2.setIsPrimitive("1");
      this.linkmanUpdateHistoryDAO.insertLinkmanUpdateHistory(linkman2);
    }

    return success;
  }
}